package com.jxt.myjxtmanager.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.inqbarna.tablefixheaders.TableFixHeaders;
import com.jxt.myjxtmanager.BaseActivity;
import com.jxt.myjxtmanager.R;
import com.jxt.myjxtmanager.db.Student;
import com.jxt.myjxtmanager.ui.inter.StudentMsgInter;
import com.jxt.myjxtmanager.ui.presenter.StudentMsgPresenter;
import com.jxt.myjxtmanager.ui.tableadapter.StudentMsgNexysAdapter;
import com.jxt.myjxtmanager.utils.ToastUtil;
import com.jxt.myjxtmanager.views.ClearEditText;

import java.util.List;

/**
 * Created by qm on 2017/3/3 0003.
 */
public class StudentMsgActivity extends BaseActivity implements StudentMsgInter {

    private StudentMsgPresenter presenter;
    private TableFixHeaders tableFixHeaders;
    private StudentMsgNexysAdapter baseTableAdapter;
    private List<Student> dataList;

    private ClearEditText et_clear_search;
    private ImageView btn_clear_search;

    @Override
    public void setMyAdapter() {
        presenter.dismissDialog();
        dataList = presenter.getQueryList();
        if (baseTableAdapter == null) {
            baseTableAdapter = new StudentMsgNexysAdapter(this, this, presenter);
//        baseTableAdapter.setDeviceOutLine(presenter.getDeviceOutLine());
            baseTableAdapter.setDevicesDates(dataList);
            tableFixHeaders.setAdapter(baseTableAdapter);
        } else {
            baseTableAdapter.setDevicesDates(dataList);
            baseTableAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void updateRecycler(List<Student> msgBeans) {
        dataList = msgBeans;
        baseTableAdapter.setDevicesDates(msgBeans);
        baseTableAdapter.notifyDataSetChanged();
    }

    @Override
    public void showToastMsg(String msg) {
        if (!TextUtils.isEmpty(msg)) {
            ToastUtil.showToast(this, msg);
        }
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_student_msg;
    }

    @Override
    protected void initView() {
        setToolbarTitle("学生信息");
        setToolbarTitleRight("添加学生", getResources().getColor(R.color.white));
        setToolbarNavigationIcon(R.drawable.ic_left_return);
        tableFixHeaders = (TableFixHeaders) findViewById(R.id.table);
        et_clear_search = (ClearEditText) findViewById(R.id.et_clear_search);
        btn_clear_search = (ImageView) findViewById(R.id.btn_clear_search);
        btn_clear_search.setOnClickListener(this);
        et_clear_search.setOnEditorActionListener(editorActionListener);
    }

    @Override
    public void onClickRightMenu() {
        startActivity(new Intent(StudentMsgActivity.this, AddStudentActivity.class));
    }

    @Override
    public void onClickLeftMenu() {
        super.onClickLeftMenu();
    }

    @Override
    protected void initData() {
        presenter = new StudentMsgPresenter(this, this);
        presenter.RequestData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.RequestData();
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_clear_search:
                if (verify()) {
//                    presenter.setQueryList(presenter.getFuzzyQueryFilter(presenter.getQueryList(), et_clear_search.getText().toString().trim()));
                    updateRecycler(presenter.getFuzzyQueryFilter(presenter.getQueryList(), et_clear_search.getText().toString().trim()));
                } else {
                    updateRecycler(presenter.getQueryList());
                }
                break;
        }
    }


    /**
     * 键盘 上搜索键
     */

    TextView.OnEditorActionListener editorActionListener = new TextView.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                // 先隐藏键盘
                ((InputMethodManager) v.getContext().getSystemService(Context
                        .INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0);
                if (verify()) {
                    updateRecycler(presenter.getFuzzyQueryFilter(presenter.getQueryList(), et_clear_search.getText().toString().trim()));
                } else {
                    updateRecycler(presenter.getQueryList());
                }
                return true;
            }
            return false;
        }

    };


    private boolean verify() {
        if (TextUtils.isEmpty(et_clear_search.getText().toString().trim())) {
//            showToastMsg("请输入搜索内容");
            return false;
        }
        return true;
    }
}
